package com.hzx.ssm.service.impl;

import com.hzx.ssm.dao.AdminDao;
import com.hzx.ssm.entity.Admin;
import com.hzx.ssm.entity.ResBean;
import com.hzx.ssm.service.AdminService;
import com.hzx.ssm.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private HttpServletRequest req;

    public ResBean login(String username, String password) {
        Admin admin = adminDao.findAdmin(username, password);
        if(admin != null && admin.getAvailable() == 0){
            return ResBean.error(400,"账号不可用");
        }
        if (admin != null) {
            // 生成token
            String token = JWTUtil.generateToken(admin.getUsername(), "Kuroko-Security-Basic", admin.getId());
            // 向浏览器返回token，客户端收到此token后存入cookie中，或者h5的本地存储
            HashMap<String, Object> data = new HashMap<>();
            admin.setPassword("*******");
            //返回token和用户
            data.put("token", token);
            data.put("user", admin);
            return ResBean.sucess(200, "登录成功", data);


        }

        return ResBean.error(401, "账号或密码错误");


    }

    @Override
    public ResBean updatePassword(String manager_id,String password) {
        adminDao.updatePassword(manager_id,password);
        return ResBean.sucess(200, "修改成功");
    }

    @Override
    public ResBean updateSelfMessage(Admin admin) {
        adminDao.updateSelfMessage(admin);
        return ResBean.sucess(200, "修改成功");
    }

    @Override
    public ResBean getAdminInfo(String manager_id) {
        Admin admin = adminDao.getAdminInfo(manager_id);
        admin.setPassword("*******");
        return new ResBean(200,"查找成功",admin);
    }

}
